package com.l024.meet.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.l024.framework.base.BaseFragment;
import com.l024.meet.R;
import com.l024.meet.fragment.chat.AllFriendFragment;
import com.l024.meet.fragment.chat.CallRecordFragment;
import com.l024.meet.fragment.chat.ChatRecordFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 聊天Fragment
 */
public class ChatFragment extends BaseFragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    private AllFriendFragment allFriendFragment;
    private CallRecordFragment callRecordFragment;
    private ChatRecordFragment chatRecordFragment;
    private List<Fragment> fragmentList = new ArrayList<>();
    //标题
    private String[] titles;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mTabLayout = (TabLayout) view.findViewById(R.id.mTabLayout);
        mViewPager = (ViewPager) view.findViewById(R.id.mViewPager);

        titles = new String[]{getString(R.string.text_chat_tab_title_1),getString(R.string.text_chat_tab_title_2),getString(R.string.text_chat_tab_title_3)};
        allFriendFragment = new AllFriendFragment();
        callRecordFragment = new CallRecordFragment();
        chatRecordFragment = new ChatRecordFragment();
        fragmentList.add(allFriendFragment);
        fragmentList.add(callRecordFragment);
        fragmentList.add(chatRecordFragment);
        //添加选项卡标题
        for(int i = 0;i<titles.length;i++){
            mTabLayout.addTab(mTabLayout.newTab().setText(titles[i]));
        }
        //预加载
        mViewPager.setOffscreenPageLimit(titles.length);
        mViewPager.setAdapter(new ChatPagerAdapter(getFragmentManager()));
        //选项卡绑定ViewPager
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                defTabStyle(tab,20);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //自定义样式
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //默认第一个选中
        defTabStyle(mTabLayout.getTabAt(0),20);
    }

    /**
     * 设置tab样式
     * @param tab
     * @param size
     */
    private void defTabStyle(TabLayout.Tab tab,int size){
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_tab_text,null);
        TextView tvTab = view.findViewById(R.id.tv_tab);
        tvTab.setText(tab.getText());
        tvTab.setTextColor(Color.WHITE);
        tvTab.setTextSize(size);
        tab.setCustomView(tvTab);
    }

    class ChatPagerAdapter extends FragmentStatePagerAdapter{


        public ChatPagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
